package fr.pioug.gdocs.gui;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import org.jdesktop.jdic.tray.*;

import fr.pioug.gdocs.service.*;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author a011228
 * @version
 */
public class GDocsSyncConfiguration extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private TrayIcon mTrayIcon;

	private JButton mOkButton;

	private JButton mCancelButton;

	private JTextField mFieldUsername;

	private JTextField mFieldPassword;

	private JFileChooser mFieldDirectory;

	/**
	 * @param pTrayIcon
	 * @return
	 */
	public GDocsSyncConfiguration(TrayIcon pTrayIcon) {

		// Configuration Frame
		super(ResourceService.getInstance().getValue("label.configuration"));
		mTrayIcon = pTrayIcon;
		this.getContentPane().setLayout(new BorderLayout());
		this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

		// Username
		JPanel lPanelUsername = new JPanel();
		lPanelUsername.setLayout(new BorderLayout());
		lPanelUsername.add(new JLabel(ResourceService.getInstance().getValue(
				"label.username")), BorderLayout.WEST);
		mFieldUsername = new JTextField(ConstantesService.getInstance()
				.getValue("conf.username"));
		lPanelUsername.add(mFieldUsername, BorderLayout.CENTER);

		// Password
		JPanel lPanelPassword = new JPanel();
		lPanelPassword.setLayout(new BorderLayout());
		lPanelPassword.add(new JLabel(ResourceService.getInstance().getValue(
				"label.password")), BorderLayout.WEST);
		mFieldPassword = new JTextField(ConstantesService
				.decrypt(ConstantesService.getInstance().getValue(
						"conf.password")));
		lPanelPassword.add(mFieldPassword, BorderLayout.CENTER);

		// Directory
		JPanel lPanelDirectory = new JPanel();
		lPanelDirectory.setLayout(new BorderLayout());
		lPanelDirectory.add(new JLabel(ResourceService.getInstance().getValue(
				"label.directory")), BorderLayout.WEST);
		mFieldDirectory = new JFileChooser(ConstantesService.getInstance()
				.getValue("conf.directory"));
		mFieldDirectory.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		mFieldDirectory.setFileHidingEnabled(true);
		lPanelDirectory.add(mFieldDirectory, BorderLayout.CENTER);

		JPanel lPanel = new JPanel(new BorderLayout());
		lPanel.setBorder(BorderFactory.createEtchedBorder());
		lPanel.add(lPanelUsername, BorderLayout.NORTH);
		lPanel.add(lPanelPassword, BorderLayout.CENTER);
		lPanel.add(lPanelDirectory, BorderLayout.SOUTH);

		this.getContentPane().add(lPanel, BorderLayout.NORTH);

		JPanel lButtonPanel = new JPanel();
		mOkButton = new JButton(ResourceService.getInstance().getValue(
				"label.ok"));
		mCancelButton = new JButton(ResourceService.getInstance().getValue(
				"label.cancel"));

		mOkButton.addActionListener(this);
		mCancelButton.addActionListener(this);
		lButtonPanel.add(mOkButton);
		lButtonPanel.add(mCancelButton);
		this.getContentPane().add(lButtonPanel, BorderLayout.SOUTH);
		this.pack();
		this.setLocationRelativeTo(null);

	}

	/**
	 * @param pE
	 */
	public void actionPerformed(ActionEvent pE) {
		if (pE.getSource().equals(mCancelButton)) {
			this.setVisible(false);
		} else if (pE.getSource().equals(mOkButton)) {
			this.setVisible(false);

			ConstantesService.getInstance().set("conf.directory",
					mFieldDirectory.getCurrentDirectory().getAbsolutePath());
			ConstantesService.getInstance().set("conf.username",
					mFieldUsername.getText());
			ConstantesService.getInstance().set("conf.password",
					ConstantesService.encrypt(mFieldPassword.getText()));

			ConstantesService.getInstance().save();
			mTrayIcon.displayMessage(ResourceService.getInstance().getValue(
					"message.title.save"), ResourceService.getInstance()
					.getValue("message.desc.save"), TrayIcon.INFO_MESSAGE_TYPE);
		}
	}
}
